package com.cxx.controller;

import com.aliyuncs.exceptions.ClientException;
import com.cxx.log.Log;
import com.cxx.mapper.BlogsMapper;
import com.cxx.mapper.UserMapper;
import com.cxx.pojo.Admin;
import com.cxx.pojo.Result;
import com.cxx.pojo.User;
import com.cxx.service.UserService;
import com.cxx.utils.AliOssUtil;
import com.cxx.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/person-web/api/*")
//
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogsMapper blogsMapper;
    @GetMapping("/allUsers")
    @Log
    public Result allUsers(String token){
        Claims claims=jwtUtils.parseJwt(token);
        String account= (String) claims.get("name");
        if(userMapper.selectAdmin(account)){
           List<User> userList=userMapper.selectAllUsers();
           return Result.sucess(userList);
        }
        else {
            return Result.error("NOT-ALLOW");
        }
    }
    @GetMapping("/forbiddenUser")
    @Log
    public Result forbiddenUser(String account){
        boolean update=userMapper.forbiddenUser(account);
        if(update){
            return Result.success("SUCCESS");
        }
        else {
            return Result.error("ERROR");
        }
    }
    @GetMapping("/allowUser")
    @Log
    public Result allowUser(String account){
        boolean update=userMapper.allowUser(account);
        if(update){
            return Result.success("SUCCESS");
        }
        else {
            return Result.error("ERROR");
        }
    }
    @GetMapping("/adminAllBlogs")
    @Log
    public Result adminAllBlogs(){
        return Result.sucess(blogsMapper.blogsList());
    }
    @GetMapping("/adminInfo")
    @Log
    public Result adminInfo(String token){
        Claims claims=jwtUtils.parseJwt(token);
        String account=(String) claims.get("name");
        Admin admin=userMapper.selectByAdminAccount(account);
        return Result.sucess(admin);
    }

    @PostMapping("/adminUpdate")
    @Log
        public void adminUpdate(MultipartFile image,Admin admin,String token) throws IOException, ClientException {
        Claims claims=jwtUtils.parseJwt(token);
        String account=(String) claims.get("name");
        admin.setAdminName(account);
            if(image!=null){
                //修改图片，删除之前的图片，并修改信息
                Admin adminBefore= userMapper.selectByAdminAccount(account);
                String imgBefore=adminBefore.getAdminImgurl();
                if(imgBefore!=null){
                    aliOssUtil.deleteImg(imgBefore);
                }
                String imgurl=aliOssUtil.upload(image);
                admin.setAdminImgurl(imgurl);
                userService.updateAdmin(admin);
            }else {
                //不修改图片，直接修改信息
                userService.updateAdmin(admin);
            }

        }
    }


